package com.party.controller.sys.user;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.Document;
import com.party.model.UserInfo;
import com.party.service.info.InfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统用户
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-31
 * Time: 下午3:18
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/sys/userinfo")
public class SysUserInfoController {

    @Resource
    InfoService infoService;

    private int pageSize = 9;

    /**
     * 用户列表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(
            @RequestParam(value = "id")Integer id,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("uid",id);
        modelAndView.setViewName("/sys/user/user_info");
        return modelAndView;
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String uid = req.getParameter("uid");
        String title = req.getParameter("title");
        String type = req.getParameter("type");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        UserInfo userInfo = new UserInfo();
        if(!"".equals(uid)&&uid!=null){
            userInfo.setUid(Integer.parseInt(uid));
        }

        userInfo.setTitle(title);
        userInfo.setType(type);
        Page p = this.infoService.getUserInfoList(userInfo, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    @RequestMapping(value = "/edit")
    public ModelAndView edit(
            @RequestParam("id") Integer id,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();
        UserInfo userinfo = null;
        if(id != null && id > 0)
        {
            userinfo = infoService.getUserInfoByPrimaryKey(id);
        }
        modelAndView.addObject("userinfo", userinfo);
        byte[]binaryContent=userinfo.getContent();
        String content="";
        if(binaryContent!=null){
            content=new String(binaryContent);
        }

        modelAndView.addObject("content",content);
        modelAndView.setViewName("sys/user/user_info_edit");
        return modelAndView;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/editData")
    @ResponseBody
    public Map<String,Object> editData(
            @RequestParam(value = "id")Integer id,
            @RequestParam(value = "title")String title,
            @RequestParam(value = "content")String content,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {
        UserInfo userInfo=new UserInfo();
        userInfo.setId(id);
        userInfo.setTitle(title);
        userInfo.setContent(content.getBytes());
        String msg="";
        int num=infoService.updateUserInfoByPrimaryKey(userInfo);
        if(num>0){
            msg="编辑成功";
        }else{
            msg="编辑失败";
        }

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("msg",msg);

        return map;
    }


    @RequestMapping(value = "content")
    public ModelAndView content(
            HttpSession session,
            HttpServletRequest request
    ) {
        ModelAndView modelAndView = new ModelAndView();
        UserInfo userInfo = new UserInfo();
        String idStr = request.getParameter("id");
        if(idStr != null && !"".equals(idStr) && Tools.isNumber(idStr))
        {
            Integer id = Integer.parseInt(idStr);
            userInfo = this.infoService.getUserInfoByPrimaryKey(id);
        }
        modelAndView.addObject("userinfo",userInfo);
        String content=new String(userInfo.getContent());
        modelAndView.addObject("content",content);
        modelAndView.setViewName("/sys/user/user_info_content");
        return modelAndView;
    }

    /**
     * 删除用户
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> del(
            @RequestParam("ids") String ids
    ) {
        int num = 0;
        try {
            num = infoService.deleteExamByIds(ids);
        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }

}
